package com.wxlsoft.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;

import com.wxlsoft.config.ConfigImportHandler;
import com.wxlsoft.excel.convert.ConvertHandler;
import com.wxlsoft.excel.core.RunHandler;
import com.wxlsoft.excel.enums.HandlerConfigType;
import com.wxlsoft.excel.enums.HandlerType;
import com.wxlsoft.excel.extract.ExtractHandler;
import com.wxlsoft.ui.conflist.ConfigTreeView;

public class OkOnPressListener implements ActionListener {

	private ExecutorService pool = Executors.newFixedThreadPool(1);
	
	private JProgressBar progressBar1;
	
	private List<LabelEntity> labels;
	
	private JTabbedPane tabbedPane1;
	
	public OkOnPressListener(JProgressBar progressBar1, JTabbedPane tabbedPane1, List<LabelEntity> labels) {
		this.progressBar1 = progressBar1;
		this.labels = labels;
		this.tabbedPane1 = tabbedPane1;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			Component component = tabbedPane1.getSelectedComponent();
			HandlerType handlerType = HandlerType.parse(component.getName());
			
			String filePath = labels.stream()
					.filter(l -> l.getHandlerType() == handlerType)
					.findFirst()
					.get().getLabel().getText();
			
			RunHandler handler = null;
			switch (handlerType) {
				case EXTRACT: 
					xmlCheck(filePath);
					handler = new ExtractHandler(progressBar1, filePath);
					break;
				case CONVERT: 
					xmlCheck(filePath);
					handler = new ConvertHandler(progressBar1, filePath);
					break;
				case CONFIG: {
					JTabbedPane ccPanel = ((JTabbedPane) ((JPanel) ((JToolBar) component).getComponent(0)).getComponent(0));
					Component cc = ccPanel.getSelectedComponent();
					HandlerConfigType ccType = HandlerConfigType.parse(cc.getName());
					
					ConfigImportHandler importHandler = new ConfigImportHandler(progressBar1, filePath, ccType);
					ConfigTreeView trees = new ConfigTreeView("拆组包配置清单", filePath);
					trees.showtree(importHandler);
				};
				break;
				default: handler = null;
					break;
			}
			
			if (Objects.isNull(handler)) return;
			
			pool.execute(new ActionRunnable(handler));
		} catch (Exception exp) {
			progressBar1.setForeground(Color.RED);
			ShowErrorMessage.error(exp);
		}
	}

	private void xmlCheck(String filePath) {
		File file = new File(filePath);
		if (!file.exists() 
				|| (!file.getName().endsWith(".xlsx") && !file.getName().endsWith(".xls"))) {
			ShowErrorMessage.show(String.format("%s 文件不合法，请选择正确的文件类型", file.getName()));
			return;
		}
	}
	
	public static class ActionRunnable implements Runnable {

		private final RunHandler handler;
		
		public ActionRunnable(RunHandler handler) {
			this.handler = handler;
		}
		
		@Override
		public void run() {
			JProgressBar jProgress = handler.getJProgress();
			try {
				jProgress.setForeground(Color.GRAY);
				handler.run();
				jProgress.setForeground(Color.GREEN);
			} catch (Exception exp) {
				jProgress.setForeground(Color.RED);
				ShowErrorMessage.error(exp);
			}
		}
		
	}
	
}
